home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / object oriented files / GNUEditApp.cp < prev    next >
Encoding:
Text File  |  1993-11-17  |  23.0 KB  |  964 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     GNUEditApp.c
  3.     
  4.     Application methods for a tiny editor.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.  
  8.  ******************************************************************************/
  9.  
  10. #include <CAppleEvent.h>
  11. #include <Commands.h>
  12. #include <CBartender.h>
  13. #include <Aliases.h>
  14. #include <Global.h>
  15. #include <Exceptions.h>
  16. #include <Folders.h>
  17. #include <CResFile.h>
  18. #include <TBUtilities.h>
  19. #include <CScrollPane.h>
  20. #include <CDialogText.h>
  21. #include <CDialog.h>
  22. #include <CPaneBorder.h>
  23. #include <CApplication.h>
  24. #include <CTextEnvirons.h>
  25. #include <CButton.h>
  26. #include <CCheckBox.h>
  27. #include <CRadioControl.h>
  28. #include <CRadioGroupPane.h>
  29. #include <CDialogText.h>
  30. #include <CPopupMenu.h>
  31. #include <CPopupPane.h>
  32. #include <CArrowPopupPane.h>
  33. #include <CBartender.h>
  34. #include <CTable.h>
  35. #include <CDataFile.h>
  36. #include <CAbstractText.h>
  37.  
  38. #include "CObjectDoc.h"
  39. #include "GNUEditApp.h"
  40. #include "CDRVRBrowser.h"
  41. #include "CPathArray.h"
  42. #include "CPathDirector.h"
  43. #include "CBrowseResDoc.h"
  44. #include "GNUEditDoc.h"
  45. #include "CDRVRDirector.h"
  46. #include "CAboutDirector.h"
  47. #include "CConfigDirector.h"
  48.  
  49. void GNUEditApp::ForceClassReferences( void)
  50. {
  51.     Boolean alwaysFalse = FALSE;
  52.     CObject *dummy;
  53.     
  54.     if (alwaysFalse)
  55.     {
  56.         member( dummy, CButton);
  57.         member( dummy, CCheckBox);
  58.         member( dummy, CRadioControl);
  59.         member( dummy, CRadioGroupPane);
  60.         member( dummy, CDialogText);
  61.         member( dummy, CPopupMenu);
  62.         member( dummy, CPopupPane);
  63.         member( dummy, CArrowPopupPane);
  64.         member( dummy, CScrollPane);        
  65.     }
  66. }
  67.  
  68. #define cmdLD  10003
  69. #define cmdLDR    10004
  70. #define cmdAR 10005
  71. #define configCPP 10100
  72. #define configCC1 10101
  73. #define configAS  10102
  74. #define configLD  10103
  75. #define configSEARCH 9998
  76.  
  77. extern    OSType            gSignature;
  78. extern    CBartender         *gBartender;
  79. extern    CApplication     *gApplication;    /* The application */
  80. extern    CDesktop        *gDesktop;
  81.  
  82. CPathArray *gStrings;
  83. short        prefsRefNum;
  84.  
  85. #define        kExtraMasters        10
  86. #define        kRainyDayFund        45000
  87. #define        kCriticalBalance    40000
  88. #define        kToolboxBalance        20000
  89. #define        kStackSize            32000
  90.  
  91. /***
  92.  * IGNUEditApp
  93.  *
  94.  *    Initialize the application. Your initialization method should
  95.  *    at least call the inherited method. If your application class
  96.  *    defines its own instance variables or global variables, this
  97.  *    is a good place to initialize them.
  98.  *
  99.  ***/
  100.  
  101. void GNUEditApp::IGNUEditApp(void)
  102.  
  103. {
  104.     SetApplLimit((Ptr) (CurStackBase-kStackSize));
  105.     CApplication::IApplication( kExtraMasters, kRainyDayFund,
  106.                         kCriticalBalance, kToolboxBalance);
  107.     for (ccnt = 8; ccnt--; ) nxt[ccnt] = GetCursor(128+ccnt);
  108. }
  109.  
  110. /* document searching */
  111.  
  112. static FSSpec    FileToFind;
  113. static GNUEditDoc *FoundDoc;
  114.  
  115. static void GNUEditSearch(CObject *theObject)
  116.     {
  117.     register CWindow    *theWindow = (CWindow *)theObject;
  118.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  119.     if (member(theDirector, GNUEditDoc)) 
  120.         {
  121.         GNUEditDoc *theDocument = (GNUEditDoc *)theDirector;
  122.         Str255 theTitle;
  123.         theWindow->GetTitle(theTitle);
  124.         if(EqualString(FileToFind.name,theTitle,TRUE,TRUE))
  125.             {
  126.             FoundDoc = theDocument;
  127.             }
  128.         }
  129.     }
  130.  
  131. static CDRVRDirector *FoundDRVR;
  132.  
  133. static void CDRVRSearch(CObject *theObject)
  134.     {
  135.     register CWindow    *theWindow = (CWindow *)theObject;
  136.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  137.     if (member(theDirector, CDRVRDirector)) 
  138.         {
  139.         CDRVRDirector *theDocument = (CDRVRDirector *)theDirector;
  140.         Str255 theTitle;
  141.         theWindow->GetTitle(theTitle);
  142.         if(EqualString(FileToFind.name,theTitle,TRUE,TRUE))
  143.             {
  144.             FoundDRVR = theDocument;
  145.             }
  146.         }
  147.     }
  148.  
  149. static CObjectDoc *FoundAR;
  150.  
  151. static void CARSearch(CObject *theObject)
  152.     {
  153.     register CWindow    *theWindow = (CWindow *)theObject;
  154.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  155.     if (member(theDirector, CObjectDoc)) 
  156.         {
  157.         CObjectDoc *theDocument = (CObjectDoc *)theDirector;
  158.         Str255 theTitle;
  159.         theWindow->GetTitle(theTitle);
  160.         if(EqualString(FileToFind.name,theTitle,TRUE,TRUE))
  161.             {
  162.             FoundAR = theDocument;
  163.             }
  164.         }
  165.     }
  166.  
  167. static Handle FoundDRVRBrowser;
  168.  
  169. static void CDRVRBrowserSearch(CObject *theObject)
  170.     {
  171.     register CWindow    *theWindow = (CWindow *)theObject;
  172.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  173.     if (member(theDirector, CDRVRBrowser)) 
  174.         {
  175.         CDRVRBrowser *theDocument = (CDRVRBrowser *)theDirector;
  176.         tResourceInfo    resInfo;
  177.         Cell            selectedCell;
  178.         // determine the selected cells
  179.                 
  180.         selectedCell.h = selectedCell.v = 0;
  181.         while (theDocument->GetSelect( kCurrentOrNext, &selectedCell, &resInfo))
  182.             {
  183.             resInfo.name[1] = *resInfo.name-1;
  184.             if(EqualString(FileToFind.name,&resInfo.name[1],TRUE,TRUE))
  185.                 {
  186.                 FoundDRVRBrowser = resInfo.resource;
  187.                 LoadResource(FoundDRVRBrowser);
  188.                 }
  189.             }
  190.         }
  191.     }
  192.  
  193. static CBrowseResDoc *FoundRes;
  194.  
  195. static void CBrowseResSearch(CObject *theObject)
  196.     {
  197.     register CWindow    *theWindow = (CWindow *) theObject;
  198.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  199.     if (member(theDirector, CBrowseResDoc)) 
  200.         {
  201.         FoundRes = (CBrowseResDoc *)theDirector;
  202.         }
  203.     }
  204.  
  205. static void SearchPaths(FSSpec *myfile)
  206.     {
  207.     Boolean strict1,strict = TRUE;
  208.     do
  209.         {
  210.         FSSpec itemPtr;
  211.         Boolean    targetIsFolder;
  212.         Boolean    wasAliased;
  213.         OSErr    iErr;
  214.         long cnt = gStrings->GetNumItems();
  215.         long i = 1;
  216.         do
  217.             {
  218.             gStrings->GetItem( &itemPtr, i);
  219.             BlockMove(FileToFind.name, itemPtr.name, 1+*FileToFind.name);
  220.             iErr = ResolveAliasFile(&itemPtr,TRUE,&targetIsFolder,&wasAliased);
  221.             }
  222.         while ((iErr || targetIsFolder || 
  223.             !EqualString(FileToFind.name, itemPtr.name, strict, TRUE)) && (++i <= cnt));
  224.         if (i <= cnt)
  225.             {
  226.             *myfile = itemPtr;
  227.             return;
  228.             }
  229.         strict1 = strict;
  230.         strict = FALSE;
  231.         }
  232.     while (strict != strict1);
  233.     myfile->name[0] = 0;
  234.     }
  235.  
  236. Handle SearchForName(FSSpec *myfile);
  237. #define O_TEXT        0x0100
  238. #define O_BINARY    0x0000
  239. #define O_RESOURCE    0x1000
  240. Handle SearchForName(FSSpec *myfile)
  241.     {
  242.     FileToFind = *myfile;
  243.     FoundDoc = 0;
  244.     gDesktop->itsWindows->DoForEach(GNUEditSearch);
  245.     if (FoundDoc)
  246.         {
  247.         myfile->vRefNum = O_TEXT;
  248.         return ((CAbstractText *)(FoundDoc->itsMainPane))->GetTextHandle();    
  249.         }
  250.     FoundDRVR = 0;
  251.     gDesktop->itsWindows->DoForEach(CDRVRSearch);
  252.     if (FoundDRVR)
  253.         {
  254.         myfile->vRefNum = O_RESOURCE;
  255.         return FoundDRVR->itsData;    
  256.         }
  257.     FoundAR = 0;
  258.     gDesktop->itsWindows->DoForEach(CARSearch);
  259.     if (FoundAR)
  260.         {
  261.         myfile->vRefNum = 0;
  262.         return FoundAR->itsData;    
  263.         }
  264.     FoundDRVRBrowser = 0;
  265.     gDesktop->itsWindows->DoForEach(CDRVRBrowserSearch);
  266.     if (FoundDRVRBrowser)
  267.         {
  268.         myfile->vRefNum = O_RESOURCE;
  269.         return FoundDRVRBrowser;    
  270.         }
  271.     SearchPaths(myfile);
  272.     return 0;
  273.     }
  274.  
  275. static void copyresfile(short in, short out)
  276.     {
  277.     short numTypes,currIndex;
  278.     UseResFile(in);
  279.     numTypes = Count1Types();
  280.     for( currIndex = 1; currIndex <= numTypes; currIndex++)
  281.         {
  282.         short    numRes,Index;
  283.         ResType    theType;
  284.         Get1IndType( &theType, currIndex);
  285.         FailOSErr(ResError());
  286.         numRes = Count1Resources( theType);
  287.         if (theType != 'UNIX') for( Index = 1; Index <= numRes; Index++)
  288.             {
  289.             short    rID;
  290.             Str255    rName;
  291.             Handle    aResource;
  292.             Handle resource = Get1IndResource( theType, Index);
  293.             FailNIL(resource);
  294.             GetResInfo(resource,&rID,&theType,rName);
  295.             DetachResource(resource);
  296.             if ((theType == 'SIZE') && (rID >= -1) && (rID <= 1))
  297.                 {
  298.                 Handle pref_resource;
  299.                 extern long atol(char *);
  300.                 struct    size
  301.                     {
  302.                     unsigned short mask;
  303.                     long size,minsize;
  304.                     } temp;
  305.                 int i;
  306.                 temp.mask = 0;
  307.                 for (i = 99; i < 115; i++)
  308.                     {
  309.                     temp.mask <<= 1;
  310.                     temp.mask |= gOptions[i]&1;
  311.                     }
  312.                 pref_resource = GetResource('PREF', 3);
  313.                 FailNILRes(pref_resource);
  314.                 temp.minsize = 1024L*atol(*pref_resource);
  315.                 pref_resource = GetResource('PREF', rID+4);
  316.                 FailNILRes(pref_resource);
  317.                 temp.size = 1024L*atol(*pref_resource);
  318.                 BlockMove(&temp, *resource, sizeof(struct size));
  319.                 }
  320.  
  321.             UseResFile(out);
  322.  
  323.             aResource = Get1Resource(theType,rID);
  324.             if (!aResource)
  325.                 {
  326.                 AddResource(resource,theType,rID,rName);
  327.                 }
  328.             else
  329.                 {
  330.                 SetHandleSize(aResource, 0);
  331.                 HandAndHand(resource, aResource);
  332.                 ChangedResource(aResource);
  333.                 DisposHandle(resource);
  334.                 }
  335.             UseResFile(in);
  336.             }        
  337.         }
  338.     }
  339.  
  340. static Str255 defaultLibname;
  341.         
  342. void CreateGlobalDocument(Handle text);
  343. void CreateGlobalDocument(Handle text)
  344.     {
  345.     long newsize;
  346.     CWindow *newwin;
  347.     Str255    name;
  348.     BlockMove(*text, name, 1+**text);
  349.     newsize = GetHandleSize(text)-*name-1;
  350.     BlockMove(1+*name+*text, *text, newsize);
  351.     SetHandleSize(text, newsize);
  352.     if (EqualString(name,"\plibrary.a_supersede",TRUE,TRUE))
  353.         {
  354.         FSSpec myfile;
  355.         FInfo info;
  356.         short in;
  357.         SFReply        macSFReply;                /* Standard File reply record        */
  358.         CDataFile    *itsFile;
  359.         Point            corner;                /* Top left corner of dialog box    */
  360.         StringHandle    prompt;                /* Prompt string                    */
  361.         
  362.                                         /* Center dialog box on the screen    */
  363.         FindDlogPosition('DLOG', putDlgID, &corner);
  364.         
  365.         prompt = GetString(STRprompt);
  366.         FailNILRes( prompt);
  367.     
  368.         MoveHHi( (Handle) prompt);
  369.         HLock((Handle) prompt);
  370.         
  371.         if (*defaultLibname < 7) BlockMove("\plibrary_____", name, 16);
  372.         else BlockMove(defaultLibname, name, *defaultLibname);
  373.         BlockMove(".a", &name[*name-6], 2);
  374.         *name -= 5;
  375.  
  376.         SFPPutFile(corner, *prompt, name, NULL, &macSFReply, putDlgID, NULL);
  377.         HUnlock((Handle) prompt);
  378.         if (!macSFReply.good) return;        
  379.         itsFile = new(CDataFile);
  380.         itsFile->IDataFile();
  381.         itsFile->SFSpecify(&macSFReply);
  382.         if (itsFile->ExistsOnDisk()) itsFile->ThrowOut();
  383.         
  384.         itsFile->CreateNew(gSignature, 'AR  ');
  385.         itsFile->Open(fsRdWrPerm);
  386.         itsFile->WriteAll(text);
  387.         itsFile->Dispose();        
  388.         }
  389.     else if (EqualString(name,"\pa.out",TRUE,TRUE))
  390.         {
  391.         FSSpec myfile;
  392.         FInfo info;
  393.         short in;
  394.         short last = CurResFile();
  395.         SFReply        macSFReply;                /* Standard File reply record        */
  396.         CResFile    *itsFile,*userFile;
  397.         Point            corner;                /* Top left corner of dialog box    */
  398.         StringHandle    prompt;                /* Prompt string                    */
  399.         
  400.                                         /* Center dialog box on the screen    */
  401.         FindDlogPosition('DLOG', putDlgID, &corner);
  402.         
  403.         prompt = (StringHandle)GetResource('PREF', 2);
  404.         FailNILRes( prompt);
  405.         *name = GetHandleSize((Handle) prompt);        
  406.         BlockMove(*(Handle)prompt, name+1, *name);
  407.         
  408.         prompt = GetString(STRprompt);
  409.         FailNILRes( prompt);
  410.     
  411.         MoveHHi( (Handle) prompt);
  412.         HLock((Handle) prompt);
  413.         SFPPutFile(corner, *prompt, name, NULL, &macSFReply, putDlgID, NULL);
  414.         HUnlock((Handle) prompt);
  415.         if (!macSFReply.good) return;
  416.         itsFile = new(CResFile);
  417.         itsFile->IResFile();
  418.         itsFile->SFSpecify(&macSFReply);
  419.         if (itsFile->ExistsOnDisk()) itsFile->ThrowOut();
  420.         
  421.         prompt = (StringHandle)GetResource('PREF', 1);
  422.         FailNILRes( prompt);        
  423.         BlockMove(*(Handle) prompt, name, 4);
  424.         
  425.         itsFile->CreateNew(*(long *)name, 'APPL');
  426.         itsFile->Open(fsRdWrPerm);
  427.         
  428.         userFile = new(CResFile);
  429.         userFile->IResFile();
  430.         
  431.         prompt = (StringHandle)GetResource('PREF', 6);
  432.         FailNILRes( prompt);
  433.         *FileToFind.name = GetHandleSize((Handle) prompt);        
  434.         BlockMove(*(Handle)prompt, FileToFind.name+1, *FileToFind.name);
  435.         SearchPaths(&myfile);
  436.         userFile->SpecifyFSSpec(&myfile);
  437.         if (userFile->ExistsOnDisk())
  438.              {
  439.              userFile->Open(fsRdPerm);
  440.              copyresfile(userFile->refNum, itsFile->refNum);
  441.              }
  442.         userFile->Dispose();
  443.         in = OpenResFile("\ploader");
  444.         if (in == -1) FailOSErr(ResError());
  445.         copyresfile(in, itsFile->refNum);
  446.  
  447.         itsFile->MakeCurrent();
  448.         AddResource(text, 'UNIX', 128, "\pRelocatable code module");
  449.         itsFile->Dispose();        
  450.         UseResFile(last);
  451.         }
  452.     else if ((name[*name]=='o') && (name[*name-1]=='.'))
  453.         {
  454.         Handle stub = GetResource('UNIX', 12);
  455.         DetachResource(stub);
  456.         FailOSErr(HandAndHand(text,stub));
  457.         FoundRes = 0;
  458.         gDesktop->itsWindows->DoForEach(CBrowseResSearch);
  459.         if (FoundRes) FoundRes->AddType(stub, 'DRVR', name);
  460.         else
  461.             {
  462.             CDRVRDirector *dialog = new( CDRVRDirector);
  463.             dialog->NewDocument( stub, gApplication, name );
  464.             dialog->BeginDialog();            
  465.             }
  466.         }
  467.     else 
  468.         {
  469.         ((GNUEditApp *)gApplication)->CreateDocumentfromHandle(text);
  470.         newwin = gDesktop->GetTopWindow();
  471.         newwin->SetTitle(name);
  472.         }
  473.     }
  474.  
  475. void GNUEditApp::SpinCursor(void)
  476.     {
  477.     SetCursor(*nxt[7&++ccnt]);
  478.     }
  479.  
  480. /***
  481.  * SetUpFileParameters
  482.  *
  483.  *    In this routine, you specify the kinds of files your
  484.  *    application opens.
  485.  *
  486.  *
  487.  ***/
  488.  
  489. void GNUEditApp::SetUpFileParameters(void)
  490.  
  491. {
  492.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  493.  
  494.         /**
  495.          **    sfNumTypes is the number of file types
  496.          **    your application knows about.
  497.          **    sfFileTypes[] is an array of file types.
  498.          **    You can define up to 4 file types in
  499.          **    sfFileTypes[].
  500.          **
  501.          **/
  502.  
  503.     sfNumTypes = 4;
  504.     sfFileTypes[0] = 'TEXT';
  505.     sfFileTypes[1] = 'AR  ';
  506.     sfFileTypes[2] = 'DFIL';
  507.     sfFileTypes[3] = 'rsrc';
  508.  
  509.         /**
  510.          **    Although it's not an instance variable,
  511.          **    this method is a good place to set the
  512.          **    gSignature global variable. Set this global
  513.          **    to your application's signature. You'll use it
  514.          **    to create a file (see CFile::CreateNew()).
  515.          **
  516.          **/
  517.  
  518.     gSignature = 'JRRK';
  519. }
  520.  
  521.  
  522. /***
  523.  * SetUpMenus
  524.  *
  525.  *    In this method, you add special menu items and set the
  526.  *    menu item dimming and checking options for your menus.
  527.  *    The most common special menu items are the names of the
  528.  *    fonts. For this tiny editor, you also want to set up the
  529.  *    dimming and checking options so only the current font
  530.  *    and size are checked.
  531.  *
  532.  ***/
  533.  
  534. void GNUEditApp::SetUpMenus(void)
  535.  
  536. {
  537.         /**
  538.          ** Let the default method read the menus from
  539.          **    the MBAR 1 resource.
  540.          **
  541.          **/
  542.  
  543.     inherited::SetUpMenus();
  544.  
  545.         /**
  546.          ** Add the fonts in the  system to the
  547.          **    Font menu. Remember, MENUfont is one
  548.          **    of the reserved font numbers.
  549.          **
  550.          **/
  551.  
  552.     AddResMenu(GetMHandle(MENUfont), 'FONT');
  553.  
  554.         /**
  555.          **    The UpdateMenus() method sets up the dimming
  556.          **    for menu items. By default, the bartender dims
  557.          **    all the menus, and each bureaucrat is reponsible
  558.          **    for turning on the items that correspond to the commands
  559.          **    it can handle.
  560.          **
  561.          **    Set up the options here. The edit pane's UpdateMenus()
  562.          **    method takes care of doing the work.
  563.          **
  564.          **    For Font and Size menus, you want all the items to
  565.          **    be enabled all the time. In other words, you don't
  566.          **    want the bartender to ever dim any of the items
  567.          **    in these two menus.
  568.          **
  569.          **/
  570.  
  571.     gBartender->SetDimOption(MENUfont, dimNONE);
  572.     gBartender->SetDimOption(MENUsize, dimNONE);
  573.     gBartender->SetDimOption(MENUstyle, dimNONE);
  574.     gBartender->SetDimOption(MENUfilter, dimNONE);
  575.     gBartender->SetDimOption(MENUconfig, dimNONE);
  576.     
  577.         /**
  578.          **    For Font and Size menus, one of the items
  579.          **    is always checked. Setting the unchecking option
  580.          **    to TRUE lets the bartender know that it should
  581.          **    uncheck all the menu items because an UpdateMenus()
  582.          **    method will check the right items.
  583.          **    For the Style menu, uncheck all the items and
  584.          **    let the edit pane's UpdateMenus() method check the
  585.          **    appropriate ones.
  586.          **
  587.          **/
  588.  
  589.     gBartender->SetUnchecking(MENUfont, TRUE);
  590.     gBartender->SetUnchecking(MENUsize, TRUE);
  591.     gBartender->SetUnchecking(MENUstyle, TRUE);
  592. }
  593.  
  594.  
  595.  
  596. /***
  597.  * CreateDocument
  598.  *
  599.  *    The user chose New from the File menu.
  600.  *    In this method, you need to create a document and send it
  601.  *    a NewFile() message.
  602.  *
  603.  ***/
  604.  
  605. void GNUEditApp::CreateDocument() 
  606.     {
  607.     CreateDocumentfromHandle(NULL);
  608.     }
  609.     
  610. void GNUEditApp::CreateDocumentfromHandle(Handle theData) 
  611. {
  612.     GNUEditDoc    *theDocument = NULL;
  613.     
  614.     // In the event that creating the document fails,
  615.     // we setup an exception handler here. If any
  616.     // of the methods called within the scope of this
  617.     // TRY block fail, an exception will be raised and
  618.     // control will be transferred to the CATCH block.
  619.     // Here, the CATCH block takes care of disposing
  620.     // of the partially created document.
  621.     
  622.     TRY
  623.     {
  624.         theDocument = new(GNUEditDoc);
  625.             
  626.             /**
  627.              **    Send your document an initialization
  628.              **    message. The first argument is the
  629.              **    supervisor (the application). The second
  630.              **    argument is TRUE if the document is printable.
  631.              **
  632.              **/
  633.         
  634.         theDocument->IGNUEditDoc(this, TRUE);
  635.     
  636.             /**
  637.              **    Send the document a NewFile() message.
  638.              **    The document will open a window, and
  639.              **    set up the heart of the application.
  640.              **
  641.              **/
  642.         theDocument->NewFilefromHandle(theData);
  643.     }
  644.     CATCH
  645.     {
  646.         ForgetObject( theDocument);
  647.     }
  648.     ENDTRY;
  649. }
  650.  
  651. /***
  652.  * OpenDocument
  653.  *
  654.  *    The user chose Open… from the File menu.
  655.  *    In this method you need to create a document
  656.  *    and send it an OpenFile() message.
  657.  *
  658.  *    The macSFReply is a good SFReply record that contains
  659.  *    the name and vRefNum of the file the user chose to
  660.  *    open.
  661.  *
  662.  ***/
  663.  
  664. void GNUEditApp::OpenDocument(SFReply *macSFReply)
  665.     {
  666.     CDocument *newDoc = NULL;
  667.     TRY
  668.     {    
  669.     switch( macSFReply->fType)
  670.         {
  671.         case 'TEXT':
  672.             {
  673.             GNUEditDoc *textDoc = new(GNUEditDoc);
  674.             newDoc = textDoc;
  675.             textDoc->IGNUEditDoc(this, TRUE);
  676.             textDoc->OpenFile(macSFReply);
  677.             break;
  678.             }
  679.         case 'AR  ':
  680.             {
  681.             CObjectDoc *textDoc = new(CObjectDoc);
  682.             newDoc = textDoc;
  683.             textDoc->IObjectDoc(this, TRUE);
  684.             textDoc->OpenFile(macSFReply);
  685.             break;
  686.             }
  687.         default:
  688.             {
  689.             CBrowseResDoc *resDoc = new(CBrowseResDoc);
  690.             newDoc = resDoc;
  691.             resDoc->IBrowseResDoc( this, TRUE);
  692.             resDoc->OpenFile(macSFReply);
  693.             break;
  694.             }
  695.         }
  696.     }
  697.     CATCH
  698.     {
  699.         ForgetObject( newDoc);
  700.     }
  701.     ENDTRY;
  702. }
  703.  
  704. /******************************************************************************
  705.  StartUpAction {OVERRIDE}
  706.  
  707.         Perform any desired start up action. This method is invoked after
  708.         files selected from the Finder have been opened/printed. The number
  709.         of preloaded files is passed as a parameter. If no files were
  710.         preloaded, this default method acts as if the user selected "New"
  711.         and sends the Gopher a DoCommand(cmdNew) message.
  712.  ******************************************************************************/
  713.  
  714. void GNUEditApp::StartUpAction(short    numPreloads)
  715.     {
  716.     short    vRefNum;
  717.     long    dirID;
  718.     Str255    fileName = {"\pgcc preferences"};
  719.     OSErr    err;
  720.     Handle    optH;
  721.  
  722.     FlushEvents(everyEvent, 0);
  723.     
  724.     /* override usual method - no default window */
  725.     FailOSErr(FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,&vRefNum,&dirID));
  726.     HCreateResFile(vRefNum,dirID,fileName);
  727.     err = ResError();
  728.     prefsRefNum = HOpenResFile(vRefNum,dirID,fileName,fsCurPerm);
  729.     gStrings = new( CPathArray);
  730.     gStrings->IRes( 1024 );
  731.     
  732.     optH = GetResource( 'PREF', 20000);
  733.     BlockMove(*optH, gOptions, sizeof(gOptions));
  734.     
  735.     /* if no preferences file, assume running for the first time, display About Box */
  736.     if (!err)
  737.         {
  738.         FInfo    fndrInfo;
  739.         HGetFInfo(vRefNum,dirID,fileName,&fndrInfo);
  740.         fndrInfo.fdType = 'PREF';
  741.         fndrInfo.fdCreator = gSignature;
  742.         HSetFInfo(vRefNum,dirID,fileName,&fndrInfo);
  743.         DoCommand(256);
  744.         }
  745.     }
  746.  
  747. /******************************************************************************
  748.  DoAppleEvent {OVERRIDE}
  749.  
  750.     Respond to an AppleEvent. This method handles the four required
  751.     AppleEvents: kAEOpenApplication, kAEOpenDocuments, kAEPrintDocuments,
  752.     and kAEQuitApplication.
  753.     
  754.  ******************************************************************************/
  755.  
  756. void GNUEditApp::DoAppleEvent( CAppleEvent *anAppleEvent)
  757.     {
  758.     DescType    eventClass;
  759.     DescType    eventID;
  760.     OSErr        err;
  761.     
  762.     eventClass = anAppleEvent->GetEventClass();
  763.     eventID = anAppleEvent->GetEventID();
  764.     
  765.     if ((eventClass == kCoreEventClass) && (eventID == kAEOpenApplication))
  766.             {
  767.             if (anAppleEvent->GotRequiredParams())
  768.                     {
  769.                     anAppleEvent->SetErrorResult( noErr);
  770.                     }
  771.             }
  772.     else inherited::DoAppleEvent( anAppleEvent);
  773.     }
  774.  
  775. int gestalt_test (int);
  776.  
  777. #define MIN_MAJOR 7
  778.  
  779. main()
  780.     {
  781.     GNUEditApp *editApp;
  782.     if (gestalt_test(MIN_MAJOR) >= MIN_MAJOR)
  783.         {
  784.         editApp = new(GNUEditApp);
  785.         editApp->IGNUEditApp();
  786.         editApp->Run();
  787.         editApp->Exit();
  788.         }
  789.     }
  790.  
  791. void spin(void);
  792.  
  793. void spin(void)
  794.     {
  795.     ((GNUEditApp *)gApplication)->SpinCursor();
  796.     }
  797.  
  798. static void CDRVRList(CObject *theObject, long destHandle)
  799.     {
  800.     CWindow                *theWindow = (CWindow *)theObject;
  801.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  802.     if (member(theDirector, CDRVRDirector)) 
  803.         {
  804.         Str255 theTitle;
  805.         theWindow->GetTitle(theTitle);
  806.         theTitle[1+*theTitle] = 0;
  807.         FailOSErr(PtrAndHand(theTitle,(Handle)destHandle,2+*theTitle));
  808.         }
  809.     }
  810.  
  811. static void CARList(CObject *theObject, long destHandle)
  812.     {
  813.     CWindow                *theWindow = (CWindow *)theObject;
  814.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  815.     if (member(theDirector, CObjectDoc)) 
  816.         {
  817.         Str255 theTitle;
  818.         theWindow->GetTitle(theTitle);
  819.         theTitle[1+*theTitle] = 0;
  820.         FailOSErr(PtrAndHand(theTitle,(Handle)destHandle,2+*theTitle));
  821.         }
  822.     }
  823.  
  824. static void CDRVRBrowserList(CObject *theObject, long destHandle)
  825.     {
  826.     register CWindow    *theWindow = (CWindow *)theObject;
  827.     CDirector            *theDirector = (CDirector*) theWindow->GetSupervisor();
  828.     if (member(theDirector, CDRVRBrowser)) 
  829.         {
  830.         CDRVRBrowser *theDocument = (CDRVRBrowser *)theDirector;
  831.         tResourceInfo    resInfo;
  832.         Cell            selectedCell;
  833.         // determine the selected cells
  834.                 
  835.         selectedCell.h = selectedCell.v = 0;
  836.         while (theDocument->GetSelect( kCurrentOrNext, &selectedCell, &resInfo))
  837.             {
  838.             if (!*defaultLibname) theWindow->GetTitle(defaultLibname);
  839.             resInfo.name[1+*resInfo.name] = 0;
  840.             resInfo.name[1] = *resInfo.name-1;
  841.             FailOSErr(PtrAndHand(&resInfo.name[1],(Handle)destHandle,1+*resInfo.name));
  842.             }
  843.         }
  844.     }
  845.  
  846. void    GNUEditApp::DoCommand( long theCommand)
  847. {
  848.     CDirector     *director;
  849.     CWindow        *window;
  850.     
  851.     switch( theCommand)
  852.     {            
  853.         case 256:    /* about box */
  854.         case 257:    /* help box */
  855.         case 258:    /* COPYING */
  856.             {
  857.             CAboutDirector *dialog = NULL;
  858.             TRY
  859.             {
  860.                 dialog = new CAboutDirector;    
  861.                 dialog->IAboutDirector((short)theCommand, gApplication);
  862.                 dialog->BeginDialog();
  863.             }
  864.             CATCH
  865.             {
  866.                 ForgetObject( dialog);
  867.             }
  868.             ENDTRY;
  869.             break;
  870.             }
  871.         case configCPP :
  872.         case configCC1 :
  873.         case configAS  :
  874.         case configLD  :
  875.             {
  876.             CConfigDirector *dialog = NULL;
  877.             TRY
  878.             {
  879.                 dialog = new CConfigDirector;    
  880.                 dialog->IConfigDirector((short)theCommand, gApplication);
  881.                 dialog->BeginDialog();
  882.             }
  883.             CATCH
  884.             {
  885.                 ForgetObject( dialog);
  886.             }
  887.             ENDTRY;
  888.             break;
  889.             }
  890.         case configSEARCH:
  891.             {
  892.             CPathDirector *dialog = NULL;
  893.             TRY
  894.             {
  895.                 dialog = new CPathDirector;    
  896.                 dialog->IPathDirector();
  897.                 dialog->BeginDialog();
  898.             }
  899.             CATCH
  900.             {
  901.                 ForgetObject( dialog);
  902.             }
  903.             ENDTRY;
  904.             break;
  905.             }
  906.         case cmdLD: case cmdLDR: case cmdAR:
  907.             {
  908.             Str255    theInput,theOutput;
  909.             void munge(int, char **, int (*)(int,char **), Handle, short);
  910.             int ld_main(int, char **);
  911.             int ar_main(int, char **);
  912.             char *argv[999];
  913.             int i,argc = 0;
  914.             Handle stdin = NewHandle(0);
  915.             Handle args = NewHandle(0);
  916.             if (theCommand == cmdAR)
  917.                 {
  918.                 argv[argc++] = "ar";
  919.                 argv[argc++] = "rcs";
  920.                 argv[argc++] = "library.a";
  921.                 }
  922.             else
  923.                 {
  924.                 argv[argc++] = "ld";
  925.                 argv[argc++] = "-r";
  926.                 argv[argc++] = "-d";
  927.                 }
  928.             if (theCommand == cmdLDR)
  929.                 {
  930.                 argv[argc++] = "-o";
  931.                 argv[argc++] = "linker.o";
  932.                 }
  933.             gDesktop->itsWindows->DoForEach1(CDRVRList, (long)args);
  934.             *defaultLibname = 0;
  935.             gDesktop->itsWindows->DoForEach1(CDRVRBrowserList, (long)args);
  936.             if (theCommand != cmdAR)
  937.                 {
  938.                 gDesktop->itsWindows->DoForEach1(CARList, (long)args);
  939.                 gDesktop->itsWindows->DoForEach1(CARList, (long)args);                
  940.                 }
  941.             HLockHi(args);
  942.                 {
  943.                 char *ptr = *args;
  944.                 while (ptr < GetHandleSize(args)+*args)
  945.                     {
  946.                     argv[argc++] = ptr+1;
  947.                     ptr += 2+*ptr;
  948.                     }
  949.                 }
  950.             argv[argc] = 0;
  951.             if (theCommand == cmdAR)
  952.                 munge(argc,argv,ar_main,stdin,cmdAR);
  953.             else
  954.                 munge(argc,argv,ld_main,stdin,cmdLD);
  955.             DisposHandle(args);
  956.             DisposHandle(stdin);
  957.             break;
  958.             }
  959.         default:
  960.             inherited::DoCommand( theCommand);
  961.             break;
  962.     }
  963. }
  964.